package test.designmode.producerconsumer;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 消费者
 */
public class Consumer implements Runnable {
    private BlockingQueue<Data> queue;

    public Consumer(BlockingQueue<Data> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);  // 模拟耗时,等价于 Thread.sleep(1000)但可读性好
                Data data = queue.take();   // 取不到就一直等
                System.out.println("消费了数据:" + data.getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
